﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ETL.Business.Handlers.FreeEntryHandler;
using ETL.Business.Handlers.ProjectCategoryHandler;
using ETL.Business.Handlers.ProjectHandlers;
using ETL.Business.Handlers.ProjectTaskHandler;
using ETL.Business.Handlers.ProjectTimeHandler;
using ETL.Business.Handlers.SessionHandler;
using ETL.Web.CustomCredential;
using WebMatrix.WebData;

namespace ETL.Web.Controllers
{
    public class FreeEntryController : Controller
    {
        //
        // GET: /FreeEntry/

        public ActionResult Index(bool isAdminRole)
        {
            ViewBag.IsAdminRole = true;
            if (!isAdminRole)
            {
                var overheads = (new GetProjectOverheadList()).Execute();
                ViewBag.Overheads = overheads;
                ViewBag.IsAdminRole = false;
            }
            
            var curData = (new GetSessionDataModel()).Current();

            ViewBag.Project = (new GetProjectById()).Execute(Int32.Parse(curData.ProjectId)).ProjectName;
            ViewBag.Category = (new GetCategoryById()).Execute(Int32.Parse(curData.ProjectId),
                            Int32.Parse(curData.CategoryId)).Description;
            ViewBag.Task = (new GetProjectTaskById()).Execute(Int32.Parse(curData.ProjectId), 
                            Int32.Parse(curData.CategoryId), Int32.Parse(curData.TaskId)).Description;
            return View();
        }

        //public ActionResult Adm()
        //{
        //    //var overheads = (new GetProjectOverheadList()).Execute();
        //    //ViewBag.Overheads = overheads;
        //    var curData = (new GetSessionDataModel()).Current();
            

        //    ViewBag.Project = (new GetProjectById()).Execute(Int32.Parse(curData.ProjectId)).ProjectName;
        //    ViewBag.Category = (new GetCategoryById()).Execute(Int32.Parse(curData.ProjectId), Int32.Parse(curData.CategoryId)).Description;
        //    ViewBag.Task = (new GetProjectTaskById()).Execute(Int32.Parse(curData.ProjectId), Int32.Parse(curData.CategoryId),Int32.Parse(curData.TaskId)).Description;
        //    return View();
        //}

        public string AddFreeEntry(string date, string hours)
        {
            string result = "Success";
            // Get data from session
            //(new SetSessionTask()).Execute(taskId.ToString());

            // Add current data for row processing
            var curData = (new GetSessionDataModel()).Current();
            var curUserName = ETLCredential.UserFullName;
            var curUserId = ETLCredential.UserId;
            int i = 0;
            try
            {
                (new AddNewProjectTime()).Execute(curData.ProjectId, curData.CategoryId, curData.TaskId, curUserName,
                                                  curUserId, date, hours);
            }
            catch(Exception)
            {
                result = "Fail! Something went wrong, please try again later!";
            }
            return result;
        }
    }
}
